﻿using Flare.DB.Entity.Contract;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace Flare.Pages.ITContract
{
    public partial class Index : UserControl, IPubTypicalPage, IDisposable
    {
        public string name
        {
            get { return "Договора"; }
        }

        public static int? id_service { get; set; }
        public static int? id_provider { get; set; }
        public static int? id_department { get; set; }
        public static bool? hideClose { get; set; }

        public Index()
        {
            InitializeComponent();
            hideCloseContract.IsChecked = Properties.Settings.Default.ITContractHideClose;
            this.Loaded += (s, e) =>
            {
                if (ContractList.SelectedIndex != -1) CreateFullMenu((int)ContractList.SelectedValue);
            };
        }

        private void ContactServices_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["contract_listsDataSet"]).MethodParameters[1] = (int?)ContractServices.SelectedValue; ;
        }
        private void ContactProvider_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["contract_listsDataSet"]).MethodParameters[2] = (int?)ContractProvider.SelectedValue; ;
        }
        private void ContactDepartment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["contract_listsDataSet"]).MethodParameters[3] = (int?)ContractDepartment.SelectedValue; ;
        }
        private void ClearFilter_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContractServices.SelectedIndex = -1;
            ContractProvider.SelectedIndex = -1;
            ContractDepartment.SelectedIndex = -1;
        }
        private void hideCloseContract_CheckedUnchecked(object sender, RoutedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["contract_listsDataSet"]).MethodParameters[4] = hideCloseContract.IsChecked;
            Properties.Settings.Default.ITContractHideClose = hideCloseContract.IsChecked.Value;
        }



        public void SetSearchString(string _content)
        {
            ((ObjectDataProvider)this.Resources["contract_listsDataSet"]).MethodParameters[0] = _content;
        }

        private void CreateFullMenu(int id)
        {
            var _insert = new FunctionalPanelItems().Insert(typeof(Pages.ITContract.Create));
            var _edit = new FunctionalPanelItems().Edit(typeof(Pages.ITContract.Edit), id);

            object[] images = new object[] { _insert, _edit };
            glob.kernel.CreateFunctionalPanel(images);
        }

        public void CreateStartMenu()
        {
            var _insert = new FunctionalPanelItems().Insert(typeof(Pages.ITContract.Create));
            object[] images = new object[] { _insert };
            glob.kernel.CreateFunctionalPanel(images);
        }

        private void ListBoxItem_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            int id = ((view_contract_list)((sender as ListBoxItem).Content)).id_contract;
            glob.kernel.ShowPage(typeof(Pages.ITContract.Details),id);
            e.Handled = true;
        }

        private void ListBoxItem_MouseRightButtonDown(object sender, RoutedEventArgs e)
        {
            if (ContractList.SelectedIndex != -1 && (sender as ListBoxItem).IsSelected == true)
            {
                CreateStartMenu();
                e.Handled = true;
            }
            else
            {
                CreateFullMenu(((view_contract_list)((sender as ListBoxItem).Content)).id_contract);
            }
        }


        public void Dispose()
        {
            id_service = (int?)ContractServices.SelectedValue;
            id_provider = (int?)ContractProvider.SelectedValue;
            id_department = (int?)ContractDepartment.SelectedValue;
            hideClose = (bool?)hideCloseContract.IsChecked;
        }
    }
}
